package com.kingdee.pda.ui.out_check;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.kingdee.pda.R;
import com.kingdee.pda.entity.SnlistBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.util.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 序列号列表
 * Created by dlj
 * DATE 2021/10/27
 * TIME 17:20
 */
public class SnListActivity extends BaseTitleActivity implements View.OnKeyListener {

    private RecyclerView mListView;
    private OutCheckSnListAdapter mAdapter;
    private EditText et_orderNo;
    private List<SnlistBean> data;
    private List<SnlistBean> adapterData = new ArrayList<>();

    public static void actionStart(Context context, List<SnlistBean> data) {
        Intent intent = new Intent(context, SnListActivity.class);
        intent.putExtra("data", (Serializable) data);
        context.startActivity(intent);
    }

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_snlist);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("序列号明细");
        mListView = findViewById(R.id.list_view);
        et_orderNo = findViewById(R.id.et_orderNo);

        setEditTextScannerEvents(et_orderNo, this);

        findViewById(R.id.btn_query).setOnClickListener(this);
    }

    @Override
    protected void initData() {
        super.initData();

        data = (List<SnlistBean>) getIntent().getSerializableExtra("data");
        adapterData.clear();
        adapterData.addAll(data);
        mAdapter = new OutCheckSnListAdapter(this, adapterData);
        mListView.setLayoutManager(new LinearLayoutManager(this));
        mListView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_query:
                String queryText = et_orderNo.getText().toString().trim();
                if (TextUtils.isEmpty(queryText)) {
                    ToastUtils.showLong(this, "请输入要查询的商品条码/序列号！");
                } else {
                    adapterData.clear();
                    for (SnlistBean bean : data) {
                        if (bean.getSkuCode().contains(queryText) || bean.getSn().contains(queryText)) {
                            adapterData.add(bean);
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (!(v instanceof EditText)) {
            return false;
        }
        EditText editText = (EditText) v;
        String input = editText.getText().toString().trim();
        if ((keyCode == KeyEvent.KEYCODE_SYSRQ || keyCode == KeyEvent.KEYCODE_ENTER) && event.getAction() == KeyEvent.ACTION_UP) {
            if (v.getId() == R.id.et_orderNo) {
                return true;
            }
        }
        return false;
    }
}
